package com.masterclient.app.businessmodule.vipmodule.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.arjinmc.recyclerviewdecoration.RecyclerViewItemDecoration;
import com.eoner.baselib.fragment.FCBaseFragment;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.DensityUtil;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.view.footer.HomeClassicFooter;
import com.masterclient.app.businessmodule.vipmodule.activity.PromotionProfitActivity;
import com.masterclient.app.businessmodule.vipmodule.adapter.ProfitAdapter;
import com.masterclient.app.businessmodule.vipmodule.bean.Profit;
import com.masterclient.app.businessmodule.vipmodule.persenter.ProfitsListContract;
import com.masterclient.app.businessmodule.vipmodule.persenter.ProfitsListPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收益
 */
public class ProfitFragment extends FCBaseFragment<ProfitsListPresenter>
        implements ProfitsListContract.View {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;
    @BindView(R.id.status_iv)
    ImageView statusIv;
    @BindView(R.id.status_tv)
    TextView statusTv;
    @BindView(R.id.empty_view)
    LinearLayout emptyView;
    private int type;
    private ProfitAdapter adapter;
    private int page = 1;
    private int pageSize = 15;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (null != bundle) {
            type = bundle.getInt("type");
        }
    }

    public static ProfitFragment newInstance(int type) {
        ProfitFragment fragment = new ProfitFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.fragment_list;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        initRecycler();
    }

    /**
     * 初始化recyclerview
     */
    private void initRecycler() {
        adapter = new ProfitAdapter();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new RecyclerViewItemDecoration.Builder(getContext())
                .thickness(DensityUtil.dip2px(8))
                .color(getResources().getColor(R.color.color_EEEEEE))
                .lastLineVisible(true)
                .create());
        recyclerView.setAdapter(adapter);
        mPresenter.getProfits(true, type, page, pageSize);
        setLoadMore();
    }

    /**
     * 设置加载更多
     */
    private void setLoadMore() {
        smartRefresh.setEnableRefresh(false);
        smartRefresh.setRefreshFooter(new HomeClassicFooter(getContext()));
        smartRefresh.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getProfits(false, type, page, pageSize);
        });
    }


    @Override
    public void profits(Profit profit) {

        PromotionProfitActivity activity = (PromotionProfitActivity) getActivity();

        // 待结算
        String wait_settle_profit = profit.getWait_settle_profit();
        // 已结算
        String balance = profit.getBalance();
        // 已提现
        String settle_profit = profit.getSettle_profit();
        // 可提现
        String total_balance = profit.getTotal_balance();
        activity.setAmount(wait_settle_profit, balance, settle_profit, total_balance);
//        Profit.SelectorBean selectorBean = profit.getSelector();
//        List<Profit.SelectorBean.ProfitStatusBean> profit_status = selectorBean.getProfit_status();
//        List<String> titles = new ArrayList<>();
//        titles.add("");
//        titles.add("");
//        titles.add("");
//        if (null != profit_status) {
//            for (Profit.SelectorBean.ProfitStatusBean bean : profit_status) {
//                int val = bean.getVal();
//                if (type == val)
//                    adapter.setStatusName(bean.getName());
//                if (10 == val)
//                    titles.set(0, bean.getName());
//                else if (20 == val)
//                    titles.set(1, bean.getName());
//                else if (30 == val)
//                    titles.set(2, bean.getName());
//            }
//        }
//        activity.setViewpageTitles(titles);
        Profit.OrdersBean ordersBean = profit.getOrders();

        if (null != ordersBean && ordersBean.getList().size() > 0) {
            adapter.addData(ordersBean.getList());
            page = page + 1;
        } else {
            smartRefresh.setNoMoreData(true);
        }
        refreshEmptyStateAndMsg(R.drawable.iconempty_list, "一贫如洗的订单");
    }

    private void refreshEmptyStateAndMsg(int imgId, String msg) {
        smartRefresh.finishLoadMore();
        if (adapter.getItemCount() == 0) {
            emptyView.setVisibility(View.VISIBLE);
            statusIv.setImageResource(imgId);
            statusTv.setText(msg);
        } else {
            emptyView.setVisibility(View.GONE);
        }
    }

    @Override
    public void error(String error) {
        refreshEmptyStateAndMsg(R.drawable.iconempty_list, error);
    }

    @OnClick(R.id.empty_view)
    public void onViewClicked() {
        emptyView.setVisibility(View.GONE);
        mPresenter.getProfits(true, type, page, pageSize);
    }
}
